<template>
  <view class="goodslist-container">
    <view v-if="goodsList.length !== 0 ">
      <view v-for="(item,i) in goodsList" :key="i">
        <my-goods :goods="item" @click="gotogoodsDetail(item.goods_id)"></my-goods>
      </view>
    </view>
    <view class="none" v-else>暂无商品~~~</view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        goodsList: [],
        // 请求参数对象
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        // 数据总数
        total: 0,
        isLoad: false,

      };
    },
    onLoad(option) {
      console.log('option:', option)
      this.queryObj.query = option.query || '';
      this.queryObj.cid = option.cid || '';
      this.getGoodsList()
    },
    onReachBottom() {
      // 判断如果有数据正在加载则不发起请求
      if(this.isLoad) return
      // 判断是否加载完毕
      if(this.queryObj.pagenum * this.queryObj.pagesize < this.total) {
        this.queryObj.pagenum++
        this.getGoodsList()
      }
     
    },
    onPullDownRefresh(){
      // 重置关键数据
      this.queryObj.pagenum = 1
      this.goodsList = []
      this.total = 0
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    methods: {
      gotogoodsDetail(goods_id) {
        uni.navigateTo({
          url: '../goods_detail/goods_detail?goods_id=' + goods_id
        })
      },
      async getGoodsList(cb) {
        this.isLoad = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        console.log("商品列表结果", res)
        if (res.meta.status !== 200) return uni.$showMsg()
        cb && cb()
        this.isLoad = false
        this.goodsList = [...this.goodsList, ...res.message.goods];
        this.total = res.message.total;
      },
    },
  }
</script>

<style lang="scss">
  .none {
    height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    color: gray;
  }
</style>
